package testing;

import gestures.slideGestures.SlideGesture;
import gestures.slideGestures.SlideGestureChange;
import gestures.tapGestures.TapGesture;

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Float;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.*;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.*;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import profiles.TestProfile;

import services.GestureClient;
import services.ListenerRegistration;
import services.subscribers.GestureEventArgs;
import services.subscribers.IGestureListener;
import services.subscribers.dragGestures.IDraggingGestureListener;
import services.subscribers.slideGestures.ISlideGestureListener;
import services.subscribers.slideGestures.ISlidingGestureListener;
import services.subscribers.tapGestures.ITapGestureListener;

class Test {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		/*
		 * Server listenerServer = new Server(8080);
		 * System.out.println("Server running on port 8080");
		 * 
		 * listenerServer.setHandler(new Test()); try { listenerServer.start();
		 * listenerServer.join(); } catch (Exception e) { e.printStackTrace(); }
		 */
		/*
		 * try { String x = postData("http://MM178187-PC:8888", "Hello there!");
		 * } catch (Exception e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
		GestureClient gl;

			System.out.println(System.getProperty("user.dir"));
			if(args.length == 0)
				gl = new GestureClient("test-java", "..\\..\\config.txt");
			else 
				gl = new GestureClient("test-java", args[0]);
			
//			gl.registerListener(new ListenerRegistration("Hello subscriber!",
//					new Rectangle2D.Float(0, 0, 1, 1), new TapGestureListener()));
			
			GraphicsEnvironment ge = GraphicsEnvironment.
			   getLocalGraphicsEnvironment();
			   GraphicsDevice[] gs = ge.getScreenDevices();
			   
			final int width = gs[0].getDisplayMode().getWidth();
			final int height = gs[0].getDisplayMode().getHeight();
			
			Rectangle2D.Float captureBounds = new Rectangle2D.Float(0,0,1,1);
			
			Robot r = null;
			try {
				r = new Robot();
			} catch (AWTException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			final Robot robot = r;
			
//			gl.registerListener(new ListenerRegistration("tapListener", captureBounds, new ITapGestureListener() {
//				
//				@Override
//				public void actionPerformed(Object sender, GestureEventArgs e) {
//					if (e.getGesture() != null && e.getGesture().getClass().equals(TapGesture.class)) {
//						TapGesture testTap = (TapGesture) e.getGesture();
//						
//						Float coords = testTap.getGestureCoords()[0];
//						
//						//System.out.println( "coords:" + coords);
//						
//						if( robot != null ) {
//							robot.mouseMove((int)((double)width * coords.x), 
//											(int)((double)height * coords.y));
//						}
//					}
//					
//				}
//			}));
			
//			gl.registerListener(new ListenerRegistration("generic", captureBounds, new IGestureListener() {
//				
//				
//				@Override
//				public void actionPerformed(Object sender, GestureEventArgs e) {
//					//System.out.println("Got slide " );
//					
//					if (e.getGesture() != null && e.getGesture().getClass().equals(SlidingGesture.class)) {
//						SlidingGesture slide = (SlidingGesture) e.getGesture();
//						
//						Float coords = slide.getGestureCoords()[0];
//						
//						//System.out.println( "coords:" + coords);
//						
//						if( robot != null ) {
//							System.out.println(  "do Move");
//							/*robot.mouseMove((int)((double)width * coords.x), 
//											(int)((double)height * coords.y));*/
//						}
//					}
//					else {
//						System.out.println("Not a slide");
//					}
//					
//				}
//			}, true));
//			
			
			TestProfile p = new TestProfile(gl);
			
//			gl.registerListener(new ListenerRegistration("DragListener", captureBounds,
//					new IDraggingGestureListener() {
//						
//						@Override
//						public void actionPerformed(Object sender, GestureEventArgs e) {
//							System.out.println( "Got gesture:" + e.getGesture() );
//							
//						}
//					}));
					
			

		// Java reflection test
		/*
		 * String className = "Gestures.TapGestures.TapGesture"; try { Class c =
		 * Class.forName(className); Constructor[] consts = c.getConstructors();
		 * for(int i=0; i<consts.length; i++) { if
		 * (consts[i].getParameterTypes().length == 2) { try { Gesture tg =
		 * (Gesture)consts[i].newInstance(new Object[]{Calendar.getInstance(),
		 * null}); System.out.println(tg.getGestureTime().getTime().toString());
		 * System.out.println(tg.getClass().toString()); } catch
		 * (IllegalArgumentException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } catch (InstantiationException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } catch
		 * (IllegalAccessException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } catch (InvocationTargetException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } }
		 * 
		 * System.out.println(consts[i].toString()); } } catch
		 * (ClassNotFoundException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
	}

}
